<%--
  Created by IntelliJ IDEA.
  User: 86177
  Date: 2024/10/14
  Time: 13:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.example.wanmengnovel.Novels" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>查找结果</title>
</head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
      integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<link href="<%=request.getContextPath()%>/css/revelMain.css" rel="stylesheet">
<body>
<header>
    <nav class="navbar navbar-expand-lg bg-body-tertiary">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">挽梦言句</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse"
                    data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
                    aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                        <a class="nav-link active" aria-current="page" href="#">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Link</a>
                    </li>
                </ul>
                <ul class="navbar-nav me-auto mb-2 mb-lg-0 position-absolute top-50 end-0 translate-middle-yposition-absolute top-50 end-0 translate-middle-y"
                    style="padding-right: 20px">
                    <% String name1 = null;
                        Cookie[] cookies1 = request.getCookies();
                        for (Cookie cookie : cookies1) {
                            if ("username".equals(cookie.getName())) {
                                name1 = cookie.getValue();
                            }
                        }
                    %>
                    <%
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        System.out.println("JDBC驱动加载成功！");
                        String url = "jdbc:mysql://localhost:3306/users";
                        String user = "root";
                        String password1 = "lcw0909.";
                        Connection connection1 = DriverManager.getConnection(url, user, password1);
                        String password2 = null;
                        String sql2 = "SELECT userPassword FROM novelusers WHERE userAccount = ?";
                        PreparedStatement preparedStatement1 = connection1.prepareStatement(sql2);
                        preparedStatement1.setLong(1, Long.valueOf(name1));
// 执行查询并获取结果集
                        ResultSet resultSet1 = preparedStatement1.executeQuery();
// 处理结果集
                        if (resultSet1.next()) {
                            password2 = resultSet1.getString("userPassword");
                        }
                    %>
                    <li class="nav-item">
                        <form method="post" action="<%=request.getContextPath()%>/login" style="display:none">
                            <input type="text" name="username" value="<%=name1%>" style="display:none">
                            <input type="password" name="password" value="<%=password2%>" style="display:none">
                            <button id="return-button" style="display:none;" type="submit"></button>
                        </form>
                        <a class="nav-link" id="return-a" href="#" style="color: #99e6f8">回到我的主页</a>


                        <%--放置个人页面信息--%>

                    </li>
                    <li class="nav-item ">
                        <a class="nav-link" href="<%=request.getContextPath()%>/my-interface">我的作品</a>
                    </li>
                    <li class="nav-item ">
                        <a class="nav-link" href="<%=request.getContextPath()%>/exit-login">退出登录</a>
                    </li>
                </ul>
                <ul class=" position-absolute top-50 start-50 translate-middle">
                    <form action="<%=request.getContextPath()%>/find" method="post" class="d-flex" role="search">
                        <input name="value" class="form-control me-2" type="search"
                               placeholder="<%=request.getParameter("value")%>" aria-label="Search">
                        <button class="btn btn-outline-success" type="submit" style="width: 100px">搜索</button>
                    </form>
                </ul>


            </div>
        </div>
    </nav>
</header>

<main>
    <div class="div_main text-center">
        <div class="grid">
            <div class="p">Value</div>
            <div class="t">Name</div>
            <div class="t">Account</div>
            <div class="t" style="padding-right: 10px">Email</div>
        </div>
        <%! public Connection getConnection() throws SQLException, ClassNotFoundException {
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("JDBC驱动加载成功！");
            String url = "jdbc:mysql://localhost:3306/users";
            String user = "root";
            String password = "lcw0909.";
            Connection connection = DriverManager.getConnection(url, user, password);
            return connection;
        }%>
        <% String value = request.getParameter("value");
            ArrayList<Novels> arrayList = new ArrayList<>();
            Connection connection = getConnection();

            // 假设您想查找与value相关的小说，这里使用JOIN可能更合适，但这里保持原始逻辑
            String novelSql = "SELECT Value, id, novelUserAccount FROM novel WHERE Value LIKE ?";
            try (PreparedStatement preparedStatementNovel = connection.prepareStatement(novelSql)) {
                preparedStatementNovel.setString(1, "%" + value + "%");
                ResultSet resultSetNovel = preparedStatementNovel.executeQuery();

                while (resultSetNovel.next()) {
                    String value1 = resultSetNovel.getString("Value");
                    int id = resultSetNovel.getInt("id");
                    int userAccount = resultSetNovel.getInt("novelUserAccount");

                    String sql = "SELECT userName, userEmail FROM novelusers WHERE userAccount = " + userAccount;
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    ResultSet resultSet = preparedStatement.executeQuery();
                    while (resultSet.next()) {
                        String userName = resultSet.getString("userName");
                        String userEmail = resultSet.getString("userEmail");


                        arrayList.add(new Novels(userName, userAccount, userEmail, value1, id));
                    }
                }
            } %>
        <%for (Novels novels : arrayList) {%>
        <div class="grid">
            <div class="p"><%= novels.getValue()%>
            </div>
            <div class="t"><%= novels.getName()%>
            </div>
            <div class="t"><%= novels.getAccount()%>
            </div>
            <div class="t" style="padding-right: 10px"><%= novels.getEmail()%>
            </div>
            <%--<div class="col">
                <form action="revise.jsp" method="post">
                    <input type="hidden" name="userid-1" value=<%= managemans.getId()%>>
                    <button type="submit" class="tit1">修改</button>
                </form>
            </div>
            <div class="col">
                <form action="Delete" method="post">
                    <input type="hidden" name="userid-0" value=<%= managemans.getId()%>>
                    <button type="submit" class="tit1">删除</button>
                </form>
            </div>--%>
        </div>
        <%}%>
    </div>
</main>

</body>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-/mhDoLbDldZc3qpsJHpLogda//BVZbgYuw6kof4u2FrCedxOtgRZDTHgHUhOCVim"
        crossorigin="anonymous"></script>
</html>
